package booking;

import com.sun.org.apache.xpath.internal.operations.Bool;
import sun.security.krb5.internal.PAData;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by James.Owen on 08/02/14.
 */
public class CostCalculator {

    private HttpServletRequest request;
    private Map<String, Object> costMap = new HashMap<String, Object>();

    private Double numberOfChildren;
    private Integer dipPlatters = 0;
    private Double totalCost;
    private Integer sandwichPlatters = 0;
    private Boolean jazzyRequired;
    private Boolean partyBags;

    private PartyPackage partyPackage;


    public CostCalculator buildFromRequest(HttpServletRequest request){
        buildCostMap(request);
        parseParameters(costMap);
        getPackageCost(numberOfChildren);
        calculateTotalCost();
        return this;
    }

    private void buildCostMap(HttpServletRequest request){
        costMap.put("numberOfChildren",request.getParameter(Parameters.numberOfChildren.getName()));
        costMap.put("dipPlatters", request.getParameter(Parameters.dipPlatters.getName()));
        costMap.put("sandwichPlatters",request.getParameter(Parameters.sandwichPlatters.getName()));
        costMap.put("jazzyRequired",request.getParameter(Parameters.jazzyRequired.getName()));
        costMap.put("partyBags",request.getParameter(Parameters.partyBags.getName()));
    }

    private void parseParameters(Map<String, Object> map) {
        numberOfChildren = Double.parseDouble((String) map.get(Parameters.numberOfChildren.getName()));
        dipPlatters      = Integer.parseInt((String) map.get(Parameters.dipPlatters.getName()));
        sandwichPlatters = Integer.parseInt((String) map.get(Parameters.sandwichPlatters.getName()));
        jazzyRequired    = Boolean.parseBoolean((String)map.get(Parameters.jazzyRequired.getName()));
        partyBags        = Boolean.parseBoolean((String)map.get(Parameters.partyBags.getName()));;
//        plattersRequired = dipPlatters > 0 && sandwichPlatters > 0;
    }

    private double calculateTotalCost() {
        totalCost = numberOfChildren * partyPackage.getCost();
        if(jazzyRequired){totalCost += 10.00;}
        if(partyBags){totalCost += (numberOfChildren * 1.50);}
        totalCost += sandwichPlatters + dipPlatters * 5;
        return totalCost;
    }

    private void getPackageCost(Double numberOfChildren) {
        if(numberOfChildren <=15)  partyPackage = new PartyPackage("Small 10 - 15 children",8.00);
        if(numberOfChildren >=16 && numberOfChildren <=20) partyPackage = new PartyPackage("Medium 16 - 20",7.50);
        if(numberOfChildren >=21)  partyPackage = new PartyPackage("Large 21 - 30 children",7.00);
    }

    public Boolean getJazzyRequired() {
        return jazzyRequired;
    }
    public Boolean getPartyBags() {
        return partyBags;
    }
    public Double getCostOfPartyBags() {
        return numberOfChildren * 1.50;
    }
    public double getCostOfChildren(){
        return numberOfChildren * partyPackage.getCost();
    }
    public Integer getDipPlatters() {
        return dipPlatters;
    }
    public Integer getSandwichPlatters() {
        return sandwichPlatters;
    }
//    public Boolean getPlattersRequired() {
//        return plattersRequired;
//    }
    public Double getTotalCost() {
        return totalCost;
    }
    public Double getNumberOfChildren() {
        return numberOfChildren;
    }
    public PartyPackage getPartyPackage() {
        return partyPackage;
    }

}
